﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

using System.Web.Resources;
using Microsoft.Reporting.WebForms;

namespace WebInvoicingsys.system.storage.stockmove
{
    public partial class stockmove_recv_print : System.Web.UI.Page
    {
        //传入的参数
        string billsno, shipto;

        private void Return()
        {
            string url = "/system/storage/stockmove/stockmove_recv.aspx";

            Server.Transfer(url);
        }

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                billsno = Request.Params["billsno"].ToString();
                shipto = Request.Params["shipto"].ToString();
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            DataSet dsResult = new DataSet();

            string sql = "", error_msg = "";

            Execute exec = new Execute(Session["connectionString"].ToString());

            //构造SQL语句
            sql = @"select  a.APPID,
                            a.billsno,
                            a.billdate,
                            a.remark,
                            a.userid,
                            (select sysuser.username from sysuser where sysuser.userid=a.userid) username,
                            a.sysdate,
                            a.billto,
                            (select warehouse.warehousename from warehouse where a.billto=warehouse.warehouseid) billtoname,
                            a.shipto,
                            (select warehouse.warehousename from warehouse where a.shipto=warehouse.warehouseid) shiptoname
                        from bills a
                        where a.billsno='{0}'
                          and a.shipto='{1}'";

            sql = String.Format(sql, billsno, shipto);

            DataSet dsMaster = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (dsMaster == null)
            {
                return null;
            }

            //复制报表的数据表结构
            WebInvoicingsys.system.storage.stockmove.stockmove_recv_print_ds report_ds = new WebInvoicingsys.system.storage.stockmove.stockmove_recv_print_ds();

            DataTable dtMaster = report_ds.dtMaster.Clone();

            foreach (DataRow dr in dsMaster.Tables[0].Rows)
            {
                DataRow drnew = dtMaster.NewRow();

                drnew["billsno"] = dr["billsno"].ToString();
                drnew["APPID"] = dr["APPID"].ToString();
                drnew["billdate"] = dr["billdate"].ToString();
                drnew["remark"] = dr["remark"].ToString();
                drnew["userid"] = dr["userid"].ToString();
                drnew["username"] = dr["username"].ToString();
                drnew["sysdate"] = dr["sysdate"].ToString();
                drnew["billto"] = dr["billto"].ToString();
                drnew["billtoname"] = dr["billtoname"].ToString();
                drnew["shipto"] = dr["shipto"].ToString();
                drnew["shiptoname"] = dr["shiptoname"].ToString();

                dtMaster.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dtMaster);

            //取得明细数据
            sql = @"select c.areaid, c.storageid, c.areaname, c.storagename, a.productcode, b.productname, a.qty1, a.qty2
                      from billsdetail a, product b, v_storage c
                       where a.productcode=b.productcode
                         and a.storageid=c.storageid
                         and a.billsno='{0}'";

            sql = String.Format(sql, billsno);

            DataSet dsDetail = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (dsDetail == null)
            {
                return null;
            }

            DataTable dtDetail = report_ds.dtDetail.Clone();

            foreach (DataRow dr in dsDetail.Tables[0].Rows)
            {
                DataRow drnew = dtDetail.NewRow();

                drnew["dgvID"] = (dtDetail.Rows.Count + 1).ToString();
                drnew["areaid"] = dr["areaid"].ToString();
                drnew["storageid"] = dr["storageid"].ToString();
                drnew["areaname"] = dr["areaname"].ToString();
                drnew["storagename"] = dr["storagename"].ToString();
                drnew["productcode"] = dr["productcode"].ToString();
                drnew["productname"] = dr["productname"].ToString();
                drnew["qty1"] = Decimal.Parse(dr["qty1"].ToString());
                drnew["qty2"] = Decimal.Parse(dr["qty2"].ToString());


                dtDetail.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dtDetail);


            return dsResult;
        }

        private void Bind()
        {
            if (GetParma() == false)
            {
                Return();
            }

            DataSet ds = GetData();

            if (ds == null)
            {
                Return();
            }

            Microsoft.Reporting.WebForms.ReportDataSource rdsMaster = new ReportDataSource("dtMaster", ds.Tables[0]);
            Microsoft.Reporting.WebForms.ReportDataSource rdsDetail = new ReportDataSource("dtDetail", ds.Tables[1]);

            rvPrint.LocalReport.DataSources.Clear();
            rvPrint.LocalReport.DataSources.Add(rdsMaster);
            rvPrint.LocalReport.DataSources.Add(rdsDetail);
            rvPrint.LocalReport.Refresh();

            rvPrint.ShowPrintButton = true;

            rvPrint.LocalReport.DisplayName = "调拨出库清单";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string login = Session["login"].ToString();

                    if (login.ToUpper() != "1")
                    {
                        Response.Redirect("/default.aspx", false);
                        return;
                    }
                }
                catch
                {
                    Response.Redirect("/default.aspx", false);
                    return;
                }

                Bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Return();
        }
    }
}